package com.maruping.disk.role.service.impl;

import com.maruping.disk.menu.exception.MenuOccupyExeception;
import com.maruping.disk.mybatis.entity.PtMenuEntity;
import com.maruping.disk.mybatis.entity.PtRoleEntity;
import com.maruping.disk.mybatis.entity.PtUserEntity;
import com.maruping.disk.mybatis.flexigrid.PagingEntity;
import com.maruping.disk.mybatis.flexigrid.TblResponseEntity;
import com.maruping.disk.mybatis.ztree.ZtreeEntity;
import com.maruping.disk.role.entity.SearchRoleEntity;
import com.maruping.disk.role.repository.RoleDao;
import com.maruping.disk.role.service.RoleService;
import com.maruping.disk.user.exception.UserOccupyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;
    // 原有的方法实现...
    @Override
    public List<PtUserEntity> queryUserListByRoleUuid(String roleUuid) {
        return roleDao.queryUserListByRoleUuid(roleUuid);
    }
    @Override
    public TblResponseEntity<PtRoleEntity> queryRoleInfoListByPage(SearchRoleEntity entity, PagingEntity paging) {
        TblResponseEntity<PtRoleEntity> tblResponseEntity = new TblResponseEntity<>();
        Integer total = roleDao.queryRoleListTotal(entity);
        tblResponseEntity.setTotal(total);

        paging.deal(PtMenuEntity.class,total);
        tblResponseEntity.setPage(paging.getPage());

        List<PtRoleEntity> list = roleDao.queryRoleInfoListByPage(entity,paging);
        tblResponseEntity.setRawRecords(list);

        tblResponseEntity.deal();
        return tblResponseEntity;
    }

    @Override
    public PtRoleEntity queryRoleInfoById(String roleUuid) {
        return roleDao.queryRoleInfoById(roleUuid);
    }

    @Override
    public void modifyRoleInfoById(PtRoleEntity entity) {
        roleDao.modifyRoleInfoById(entity);
    }

    @Override
    public void addRoleInfoById(PtRoleEntity entity) {
        roleDao.addRoleInfoById(entity);
    }

    @Override
    public void deleteRoleByIds(String ids) throws UserOccupyException,MenuOccupyExeception {
        String roleArray[] = ids.split(",");
        String roleName = null;
        StringBuffer userSb = new StringBuffer();
        StringBuffer menuSb = new StringBuffer();
        for(String roleUuid:roleArray){
            roleName = roleDao.queryUserListByRole(roleUuid);
            if(roleName != null){
                userSb.append(roleName);
                userSb.append(",");
            }

            roleName = roleDao.queryMenuListByRole(roleUuid);
            if(roleName != null){
                menuSb.append(roleName);
                menuSb.append(",");
            }
        }

        String userStr= userSb.toString();
        if(!"".equals(userStr)){
            userStr = userStr.substring(0,userStr.length() - 1);
            throw  new UserOccupyException(userStr);
        }

        String menuStr= menuSb.toString();
        if(!"".equals(menuStr)){
            menuStr = menuStr.substring(0,menuStr.length() - 1);
            throw  new MenuOccupyExeception(menuStr);
        }

        roleDao.deleteRoleByIds(roleArray);

    }

    @Override
    public List<ZtreeEntity> queryMenuTreeListByRole(String roleUuid) {
        List<ZtreeEntity> list = roleDao.queryMenuTreeList();
        List<String> menulist = roleDao.queryMenuByRole(roleUuid);
        for(ZtreeEntity ztreeItem:list){
            for(String menuId:menulist){
                if (menuId.equals(ztreeItem.getId())){
                    ztreeItem.setChecked(true);
                }
            }
        }
        return list;
    }

    @Override
    public void saveRoleRefMenu(String roleUuidArray, String menuIdArray) {
        String[] roleUuidList = roleUuidArray.split(",");
        String[] menuIdList = menuIdArray.split(",");
        for (String roleUuid:roleUuidList){
            roleDao.deleteMenuByRole(roleUuid);
            for (String menuUuid:menuIdList){
                roleDao.insertMenuRefRole(roleUuid,menuUuid);
            }
        }
    }

}
